% Z = POLYADD(X,Y) Add the two polynomials X and Y
%
%   Inputs:
%
%       x,y     Polynomial summands
%
%   Outputs:
%
%       z       The sum of x and y

function z = polyAdd(x,y)

    % if x is zero, return y
    if polyIsZero(x)
        z = y;
    % if y is zero, return x        
    elseif polyIsZero(y)
        z = x;
    else
        % Determine which polynomial is shorter
        if length(x) > length(y)
            shortPoly = y;
            longPoly = x;
        else
            shortPoly = x;
            longPoly = y;
        end

        % Initialize output polynomial vector
        z = longPoly;

        % Add all coefficients
        for i = 1 : length(shortPoly)
            z(i) = elementAdd(shortPoly(i), longPoly(i));
        end
    end

    % Get rid of any leading zeros
    z = polyTrimZeros(z);
end

